package com.renwei.yunlong.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bigkoo.svprogresshud.SVProgressHUD;
import com.google.gson.Gson;
import com.renwei.yunlong.R;
import com.renwei.yunlong.base.BaseActivity;
import com.renwei.yunlong.bean.LoginBean;
import com.renwei.yunlong.bean.OwnerSLABean;
import com.renwei.yunlong.bean.WorkBean;
import com.renwei.yunlong.bean.ServiceLoginBean;
import com.renwei.yunlong.bean.ServiceSLABean;
import com.renwei.yunlong.http.HttpPresenter;
import com.renwei.yunlong.http.HttpTaskListener;
import com.renwei.yunlong.utils.ACache;
import com.renwei.yunlong.utils.ToastUtil;
import com.zyf.fwms.commonlibrary.http.Api;
import com.zyf.fwms.commonlibrary.utils.AutoUtils;
import com.zyf.fwms.commonlibrary.utils.CommonUtils;
import com.zyf.fwms.commonlibrary.utils.StatusBarUtil;

import butterknife.Bind;
import butterknife.ButterKnife;

import static com.renwei.yunlong.R.id.et_instruction;
import static com.renwei.yunlong.R.id.rl_service;

/**
 * 发起工单--页面
 * <p>
 * Created by Administrator on 2017/8/23.
 */

public class InitiatedWorkActivity extends BaseActivity implements View.OnClickListener, HttpTaskListener {

    @Bind(R.id.iv_back)
    ImageView ivBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tv_basic)
    TextView tvBasic;
    @Bind(R.id.view_basic)
    View viewBasic;
    @Bind(R.id.rl_click_basic)
    RelativeLayout rlClickBasic;
    @Bind(R.id.tv_annex)
    TextView tvAnnex;
    @Bind(R.id.view_annex)
    View viewAnnex;
    @Bind(R.id.rl_click_annex)
    RelativeLayout rlClickAnnex;
    @Bind(R.id.rl_annex)
    RelativeLayout rlAnnex;//附件列表
    @Bind(R.id.ll_basic)
    LinearLayout llBasic;//基本信息列表
    @Bind(R.id.rl_basic_btn)
    RelativeLayout rlBasicBtn;//基本信息发起按钮
    @Bind(R.id.btn_work)
    Button btnWork;
    @Bind(R.id.rl_unit)
    RelativeLayout rlUnit;//企业
    @Bind(R.id.rl_initiated_people)
    RelativeLayout rlInitiatedPeople;//发起人
    @Bind(R.id.rl_deal)
    RelativeLayout rlDeal;//处理企业
    @Bind(R.id.rl_deal_people)
    RelativeLayout rlDealPeople;//处理企业人
    @Bind(R.id.rl_service_new)
    RelativeLayout rlServiceNew;//服务对象
    @Bind(R.id.view)
    View view;

    @Bind(R.id.tv_company_name)
    TextView tvCompanyName;
    @Bind(R.id.tv_sponsor)
    TextView tvSponsor;
    @Bind(R.id.tv_deal_people)
    TextView tvDealPeople;
    @Bind(R.id.tv_deal_company_name)
    TextView tvDealCompanyName;
    @Bind(R.id.rl_project)
    RelativeLayout rlProject;
    @Bind(rl_service)
    RelativeLayout rlService;
    @Bind(R.id.view_project)
    View viewProject;
    @Bind(R.id.tv_project)
    TextView tvProject;
    @Bind(R.id.tv_service_new)
    TextView tvServiceNew;
    @Bind(R.id.btn_high)
    Button btnHigh;
    @Bind(R.id.btn_in)
    Button btnIn;
    @Bind(R.id.btn_low)
    Button btnLow;
    @Bind(R.id.tv_service)
    TextView tvService;
    @Bind(R.id.tv_type)
    TextView tvType;
    @Bind(R.id.rl_schedule)
    RelativeLayout rlSchedule;
    @Bind(R.id.view_schedule)
    View viewSchedule;
    @Bind(R.id.et_day)
    EditText etDay;
    @Bind(R.id.et_hour)
    EditText etHour;

    public ACache mCache;
    public String bindFlagStr;
    public String companyCodeStr;
    public Intent intent;
    public String contractId;
    public String companyType;

    public LoginBean loginBean;
    public ServiceLoginBean serviceLoginBean;
    @Bind(R.id.iv_unit_next)
    ImageView ivUnitNext;
    @Bind(R.id.iv_deal_next)
    ImageView ivDealNext;
    @Bind(R.id.et_describe)
    EditText etDescribe;//描述
    @Bind(et_instruction)
    EditText etInstruction;//说明
    @Bind(R.id.iv_initiated_people_next)
    ImageView ivInitiatedPeopleNext;

    String degreeValue;//紧急程度  值

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_initiated_work);
        ButterKnife.bind(this);
        mCache = ACache.get(this);
        StatusBarUtil.setColor(this, CommonUtils.getColor(this, R.color.title_color), 0);
        //屏幕自适配
        AutoUtils.setSize(this, false, 720, 1280);
        AutoUtils.auto(this);

        initData();
    }

    private void initData() {
        ivBack.setVisibility(View.VISIBLE);
        tvTitle.setText("发起工单");

        loginBean = (LoginBean) mCache.getAsObject("loginBean");
        serviceLoginBean = (ServiceLoginBean) mCache.getAsObject("serviceLoginBean");
        companyType = mCache.getAsString("companyType");

        if ("1".equals(companyType)) {//业主
            rlUnit.setEnabled(false);
            rlInitiatedPeople.setEnabled(false);

            rlProject.setVisibility(View.GONE);
            viewProject.setVisibility(View.GONE);

            rlSchedule.setVisibility(View.GONE);
            viewSchedule.setVisibility(View.GONE);

            rlDeal.setEnabled(true);//允许

            tvCompanyName.setText(loginBean.getRows().getCompany().getCompanyName());
            tvCompanyName.setTextColor(Color.parseColor("#333333"));

            ivUnitNext.setVisibility(View.GONE);
            ivDealNext.setVisibility(View.VISIBLE);
            ivInitiatedPeopleNext.setVisibility(View.GONE);

            tvSponsor.setText(loginBean.getRows().getName());
            tvSponsor.setTextColor(Color.parseColor("#333333"));

        } else if ("2".equals(companyType)) {//服务商
            rlUnit.setEnabled(true);
            rlInitiatedPeople.setEnabled(true);

            rlProject.setVisibility(View.VISIBLE);
            viewProject.setVisibility(View.VISIBLE);

            rlSchedule.setVisibility(View.VISIBLE);
            viewSchedule.setVisibility(View.VISIBLE);

            rlDeal.setEnabled(false);//禁止

            tvDealCompanyName.setText(serviceLoginBean.getRows().getCompany().getCompanyName());
            tvDealCompanyName.setTextColor(Color.parseColor("#333333"));

            ivUnitNext.setVisibility(View.VISIBLE);
            ivDealNext.setVisibility(View.GONE);

        }

        ivBack.setOnClickListener(this);
        rlClickBasic.setOnClickListener(this);
        rlClickAnnex.setOnClickListener(this);

        rlUnit.setOnClickListener(this);
        rlInitiatedPeople.setOnClickListener(this);
        rlServiceNew.setOnClickListener(this);
        rlService.setOnClickListener(this);

        rlDeal.setOnClickListener(this);
        rlDealPeople.setOnClickListener(this);

        rlProject.setOnClickListener(this);

        btnWork.setOnClickListener(this);

        btnHigh.setOnClickListener(this);
        btnIn.setOnClickListener(this);
        btnLow.setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.rl_click_basic:
                tvBasic.setTextColor(getResources().getColor(R.color.blue_color));
                viewBasic.setVisibility(View.VISIBLE);

                tvAnnex.setTextColor(getResources().getColor(R.color.text_gray_color));
                viewAnnex.setVisibility(View.GONE);

                llBasic.setVisibility(View.VISIBLE);
                rlAnnex.setVisibility(View.GONE);
                break;
            case R.id.rl_click_annex:
                tvAnnex.setTextColor(getResources().getColor(R.color.blue_color));
                viewAnnex.setVisibility(View.VISIBLE);

                tvBasic.setTextColor(getResources().getColor(R.color.text_gray_color));
                viewBasic.setVisibility(View.GONE);

                llBasic.setVisibility(View.GONE);
                rlAnnex.setVisibility(View.VISIBLE);
                break;

            case R.id.rl_unit://选择业主单位
                intent = new Intent(InitiatedWorkActivity.this, SelectServiceActivity.class);
                startActivityForResult(intent, 111);
                break;
            case R.id.rl_initiated_people://发起人
                if (TextUtils.isEmpty(bindFlagStr) && TextUtils.isEmpty(companyCodeStr)) {
                    ToastUtil.showShort(this, "请选择企业");
                } else {
                    intent = new Intent(InitiatedWorkActivity.this, SelectSponsorActivity.class);
                    intent.putExtra("bindFlag", bindFlagStr);
                    intent.putExtra("companyCode", companyCodeStr);
                    startActivityForResult(intent, 222);
                }
                break;
            case R.id.rl_deal://处理企业
                intent = new Intent(InitiatedWorkActivity.this, SelectProcessingUnitActivity.class);
                startActivityForResult(intent, 333);
                break;
            case R.id.rl_deal_people://处理人
                intent = new Intent(InitiatedWorkActivity.this, SelectDealPeopleActivity.class);
                startActivityForResult(intent, 444);
                break;
            case R.id.rl_project://所属项目
                String companyCode = mCache.getAsString("companyCode");
                if (TextUtils.isEmpty(companyCode)) {
                    ToastUtil.showShort(this, "请选择企业");
                } else {
                    intent = new Intent(InitiatedWorkActivity.this, SelectBelongsActivity.class);
                    intent.putExtra("companyCode", companyCode);
                    startActivityForResult(intent, 555);
                }
                break;
            case R.id.rl_service_new://服务对象
                contractId = mCache.getAsString("contractId");

                if (TextUtils.isEmpty(contractId)) {
                    intent = new Intent(InitiatedWorkActivity.this, SelectServiceNewActivity.class);
                    startActivityForResult(intent, 666);
                } else {
                    String code = mCache.getAsString("companyCode");
                    if (!TextUtils.isEmpty(code)) {
                        intent = new Intent(InitiatedWorkActivity.this, SelectServiceNewActivity.class);
                        intent.putExtra("companyCode", code);
                        intent.putExtra("contractId", contractId);
                        startActivityForResult(intent, 666);
                    } else {
                        ToastUtil.showShort(this, "请选择项目");
                    }
                }
                break;
            case rl_service://所属服务
                String code = mCache.getAsString("companyCode");
                String serviceHardwareId = mCache.getAsString("serviceHardwareId");

                if (!TextUtils.isEmpty(code)) {
                    if (!TextUtils.isEmpty(serviceHardwareId)) {
                        intent = new Intent(InitiatedWorkActivity.this, ServiceContentsActivity.class);
                        intent.putExtra("companyCode", code);
                        intent.putExtra("contractId", contractId);
                        intent.putExtra("serviceHardwareId",serviceHardwareId);
                        startActivityForResult(intent, 777);
                    } else {
                        ToastUtil.showShort(this, "请选择服务对象");
                    }
                } else {
                    ToastUtil.showShort(this, "请选择项目");
                }
                break;
            case R.id.btn_high:

                btnHigh.setBackgroundResource(R.drawable.shape_deal_solved_dialog_pre);
                btnHigh.setTextColor(Color.parseColor("#FFFFFF"));
                btnIn.setBackgroundResource(R.drawable.shape_deal_dialog_nor);
                btnIn.setTextColor(Color.parseColor("#333333"));
                btnLow.setBackgroundResource(R.drawable.shape_deal_unsolved_dialog_nor);
                btnLow.setTextColor(Color.parseColor("#333333"));

                degreeValue = "1";//高
                serviceHardwareId = mCache.getAsString("serviceHardwareId");
                String ownerHardwareId = mCache.getAsString("ownerHardwareId");
                contractId = mCache.getAsString("contractId");
                Log.e("返回contractId---", contractId);
                if ("1".equals(companyType)) {
                    if (loginBean != null) {
                        if (!TextUtils.isEmpty(ownerHardwareId)) {
                            //{“ownerCode”：公司code，“hardWareIds”：服务对象Id，“urgency”：紧急程度（1高2中3低）}
                            String jsonBean = "{ownerCode:\"" + loginBean.getRows().getOwnerCode() + "\",hardWareIds:\"" + ownerHardwareId + "\",urgency:\"" + degreeValue + "\"}";
                            ownerSLAHttp(jsonBean);
                        } else {
                            ToastUtil.showShort(InitiatedWorkActivity.this, "请选择服务对象");
                        }
                    }

                } else if ("2".equals(companyType)) {
                    //{"contractId":项目ID，“serviceProviderCode”：公司code,“hardWareIds”：服务对象Id，“urgency”：紧急程度（1高2中3低）} 
                    if (serviceLoginBean != null) {
                        if (!TextUtils.isEmpty(contractId)) {
                            if (!TextUtils.isEmpty(serviceHardwareId)) {
                                String jsonBean = "{contractId:\"" + contractId + "\",serviceProviderCode:\"" + serviceLoginBean.getRows().getServiceProviderCode() + "\",hardWareIds:\"" + serviceHardwareId + "\",urgency:\"" + degreeValue + "\"}";
                                serviceSLAHttp(jsonBean);
                            } else {
                                ToastUtil.showShort(InitiatedWorkActivity.this, "请选择服务对象");
                            }
                        } else {
                            ToastUtil.showShort(InitiatedWorkActivity.this, "请选择所属项目");
                        }
                    }
                }

                break;
            case R.id.btn_in:

                btnIn.setBackgroundResource(R.drawable.shape_deal_dialog_pre);
                btnIn.setTextColor(Color.parseColor("#FFFFFF"));
                btnHigh.setBackgroundResource(R.drawable.shape_deal_solved_dialog_nor);
                btnHigh.setTextColor(Color.parseColor("#333333"));
                btnLow.setBackgroundResource(R.drawable.shape_deal_unsolved_dialog_nor);
                btnLow.setTextColor(Color.parseColor("#333333"));

                degreeValue = "2";//中

                serviceHardwareId = mCache.getAsString("serviceHardwareId");
                ownerHardwareId = mCache.getAsString("ownerHardwareId");

                contractId = mCache.getAsString("contractId");
                if ("1".equals(companyType)) {
                    if (loginBean != null) {
                        if (!TextUtils.isEmpty(ownerHardwareId)) {
                            //{“ownerCode”：公司code，“hardWareIds”：服务对象Id，“urgency”：紧急程度（1高2中3低）}
                            String jsonBean = "{ownerCode:\"" + loginBean.getRows().getOwnerCode() + "\",hardWareIds:\"" + ownerHardwareId + "\",urgency:\"" + degreeValue + "\"}";
                            ownerSLAHttp(jsonBean);
                        } else {
                            ToastUtil.showShort(InitiatedWorkActivity.this, "请选择服务对象");
                        }
                    }

                } else if ("2".equals(companyType)) {
                    //{"contractId":项目ID，“serviceProviderCode”：公司code,“hardWareIds”：服务对象Id，“urgency”：紧急程度（1高2中3低）} 
                    if (serviceLoginBean != null) {
                        if (!TextUtils.isEmpty(contractId)) {
                            if (!TextUtils.isEmpty(serviceHardwareId)) {
                                String jsonBean = "{contractId:\"" + contractId + "\",serviceProviderCode:\"" + serviceLoginBean.getRows().getServiceProviderCode() + "\",hardWareIds:\"" + serviceHardwareId + "\",urgency:\"" + degreeValue + "\"}";
                                serviceSLAHttp(jsonBean);
                            } else {
                                ToastUtil.showShort(InitiatedWorkActivity.this, "请选择服务对象");
                            }
                        } else {
                            ToastUtil.showShort(InitiatedWorkActivity.this, "请选择所属项目");
                        }
                    }
                }

                break;
            case R.id.btn_low:

                btnLow.setBackgroundResource(R.drawable.shape_deal_unsolved_dialog_pre);
                btnLow.setTextColor(Color.parseColor("#FFFFFF"));
                btnIn.setBackgroundResource(R.drawable.shape_deal_dialog_nor);
                btnIn.setTextColor(Color.parseColor("#333333"));
                btnHigh.setBackgroundResource(R.drawable.shape_deal_solved_dialog_nor);
                btnHigh.setTextColor(Color.parseColor("#333333"));

                degreeValue = "3";//低

                serviceHardwareId = mCache.getAsString("serviceHardwareId");
                ownerHardwareId = mCache.getAsString("ownerHardwareId");
                contractId = mCache.getAsString("contractId");
                if ("1".equals(companyType)) {
                    if (loginBean != null) {
                        if (TextUtils.isEmpty(ownerHardwareId)) {
                            ToastUtil.showShort(InitiatedWorkActivity.this, "请选择服务对象");
                        } else {
                            //{“ownerCode”：公司code，“hardWareIds”：服务对象Id，“urgency”：紧急程度（1高2中3低）}
                            String jsonBean = "{ownerCode:\"" + loginBean.getRows().getOwnerCode() + "\",hardWareIds:\"" + ownerHardwareId + "\",urgency:\"" + degreeValue + "\"}";
                            ownerSLAHttp(jsonBean);
                            Log.e("业主  低 jsonBean", jsonBean);
                        }
                    }

                } else if ("2".equals(companyType)) {
                    //{"contractId":项目ID，“serviceProviderCode”：公司code,“hardWareIds”：服务对象Id，“urgency”：紧急程度（1高2中3低）} 
                    if (serviceLoginBean != null) {
                        if (TextUtils.isEmpty(contractId)) {
                            ToastUtil.showShort(InitiatedWorkActivity.this, "请选择所属项目");
                        } else {
                            if (TextUtils.isEmpty(serviceHardwareId)) {
                                ToastUtil.showShort(InitiatedWorkActivity.this, "请选择服务对象");
                            } else {
                                String jsonBean = "{contractId:\"" + contractId + "\",serviceProviderCode:\"" + serviceLoginBean.getRows().getServiceProviderCode() + "\",hardWareIds:\"" + serviceHardwareId + "\",urgency:\"" + degreeValue + "\"}";
                                serviceSLAHttp(jsonBean);
                                Log.e("服务商  低 jsonBean", jsonBean);
                            }
                        }
                    }
                }

                break;

            case R.id.btn_work:

                String title = etDescribe.getText().toString().trim();//描述
                String employeeId = mCache.getAsString("employeeId");//发起人id
                loginBean = (LoginBean) mCache.getAsObject("loginBean");
                String ownerCompanyCode = mCache.getAsString("ownerCompanyCode");
                String processPeopleReq = mCache.getAsString("employeeId");
                serviceHardwareId = mCache.getAsString("serviceHardwareId");
                ownerHardwareId = mCache.getAsString("ownerHardwareId");

                String detailId = mCache.getAsString("detailId");//服务目录id

                String ownerDetailId = mCache.getAsString("directoryId");

                String relationId = mCache.getAsString("relationId");

                String day = etDay.getText().toString().trim();
                String hour = etHour.getText().toString().trim();

                String serviceContractId = mCache.getAsString("contractId");

                String serviceEmployeeId = mCache.getAsString("serviceEmployeeId");

                String requestContent = etInstruction.getText().toString().trim();//详细说明

                String ownerBindFlag = mCache.getAsString("ownerBindFlag");

                if ("1".equals(companyType)) {//业主
                    /**
                     * jsonBean={
                     “title”：简要描述，(必填)
                     ”sendUser”:发起人id，(必填)
                     "ownerCode":当前公司code,(必填)
                     “serviceProviderCode”：处理企业Code，(必填)
                     “processPeopleReq”：处理人ID
                     “hardWareIds”：服务对象Id，
                     “directoryId”：服务目录ID
                     "urgency":紧急程度（1高2中3低）（必填）
                     “days”:SLA天数（必填）
                     “hours”:SLA小时（必填）注意：小时不能超过24小时
                     “requestContent”：详细说明
                     “outsourceFlag”：外协标识 0：不是外协 1： 是外协，(必填)
                     “filenames”： 附件名称集，（多个附件时，以逗号隔开）
                     “attachPath”：附件路径
                     “attachSize”：附件大小
                     "currentUserId":当前员工employeeId（必填） }

                     */
                    if ("1".equals(ownerBindFlag)){
                        String jsonBean = "{title:\"" + title + "\",sendUser:\"" + loginBean.getRows().getEmployeeId() + "\",ownerCode:\"" + loginBean.getRows().getOwnerCode() +
                                "\",serviceProviderCode:\"" + ownerCompanyCode + "\",processPeopleReq:\"" + processPeopleReq +
                                "\",hardWareIds:\"" + ownerHardwareId + "\",directoryId:\"" + ownerDetailId + "\",urgency:\"" + degreeValue + "\",days:\"" + day + "\",hours:\"" + hour +
                                "\",requestContent:\"" + requestContent + "\",outsourceFlag:\"" + "1" +"\",filenames:\"" + "" + "\",attachPath:\"" + "" +
                                "\",attachSize:\"" + "" + "\",currentUserId:\"" + loginBean.getRows().getEmployeeId() + "\"}";
                        ownerHttp(jsonBean);

                        Log.e("业主发起工单",jsonBean);

                    }else{
                        String jsonBean = "{title:\"" + title + "\",sendUser:\"" + loginBean.getRows().getEmployeeId() + "\",ownerCode:\"" + loginBean.getRows().getOwnerCode() +
                                "\",serviceProviderCode:\"" + ownerCompanyCode + "\",processPeopleReq:\"" + processPeopleReq +
                                "\",hardWareIds:\"" + ownerHardwareId + "\",directoryId:\"" + ownerDetailId + "\",urgency:\"" + degreeValue + "\",days:\"" + day + "\",hours:\"" + hour +
                                "\",requestContent:\"" + requestContent + "\",outsourceFlag:\"" + "0" +"\",filenames:\"" + "" + "\",attachPath:\"" + "" +
                                "\",attachSize:\"" + "" + "\",currentUserId:\"" + loginBean.getRows().getEmployeeId() + "\"}";
                        ownerHttp(jsonBean);

                        Log.e("业主发起工单",jsonBean);
                    }

                } else if ("2".equals(companyType)) {//服务商
                    /**
                     * jsonBean={
                     "ownerCode":业主单位code（必填）,
                     "sendUser":发起人id（必填），
                     "serviceProviderCode"：当前公司Code（必填）
                     "processPeopleReq":处理人ID
                     "hardWareIds"：服务对象Id
                     "contractId":所属项目ID
                     "relationIds":项目对象关联服务目录ID
                     "directoryId":服务目录ID
                     "urgency":紧急程度（1高2中3低）（必填）
                     "days":服务协议完成天数（必填）
                     "hours":服务协议完成小时（必填）注意：小时不能超过24小时
                     "tempCategoryId ": SLA指标模板分类ID
                     "title":简要描述（必填）
                     " requestContent":详细说明
                     "outsourceFlag":外协标识 0：不是外协 1： 是外协（必填）
                     "isInstead":是否是代发工单1是，0否（必填）
                     "currentUserId":当前员工employeeId （必填）
                     "filenames":附件名称集合
                     "attachPath":附件路径集合
                     "attachSize":附件大小集合
                     }
                     */
                    String bindFlag = mCache.getAsString("bindFlag");

                    if ("1".equals(bindFlag)){//外协标识  是
                        String jsonBean = "{ownerCode:\"" + companyCodeStr + "\",sendUser:\"" + employeeId + "\",serviceProviderCode:\"" + serviceLoginBean.getRows().getServiceProviderCode() +
                                "\",processPeopleReq:\"" + serviceEmployeeId + "\",hardWareIds:\"" + serviceHardwareId + "\",contractId:\"" + serviceContractId + "\",relationIds:\"" + relationId + "\",directoryId:\"" + detailId + "\",urgency:\"" + degreeValue + "\",days:\"" + day +
                                "\",hours:\"" + hour + "\",tempCategoryId:\"" + "" +"\",title:\"" + title + "\",requestContent:\"" + "" +
                                "\",outsourceFlag:\"" + "1" + "\",isInstead:\"" + "1" +"\",currentUserId:\"" + serviceLoginBean.getRows().getEmployeeId() + "\",filenames:\"" + "" +"\",attachPath:\"" + "" +"\",attachSize:\"" + "" + "\"}";
                        serviceHttp(jsonBean);

                    }else if ("2".equals(bindFlag)){//外协标识  否
                        String jsonBean = "{ownerCode:\"" + companyCodeStr + "\",sendUser:\"" + employeeId + "\",serviceProviderCode:\"" + serviceLoginBean.getRows().getServiceProviderCode() +
                                "\",processPeopleReq:\"" + serviceEmployeeId + "\",hardWareIds:\"" + serviceHardwareId + "\",contractId:\"" + serviceContractId + "\",relationIds:\"" + relationId + "\",directoryId:\"" + detailId + "\",urgency:\"" + degreeValue + "\",days:\"" + day +
                                "\",hours:\"" + hour + "\",tempCategoryId:\"" + "" +"\",title:\"" + title + "\",requestContent:\"" + "" +
                                "\",outsourceFlag:\"" + "0" + "\",isInstead:\"" + "0" +"\",currentUserId:\"" + serviceLoginBean.getRows().getEmployeeId() + "\",filenames:\"" + "" +"\",attachPath:\"" + "" +"\",attachSize:\"" + "" + "\"}";
                        serviceHttp(jsonBean);
                    }

                }
                break;
            default:
                break;
        }
    }


    //业主工单 SLA协议
    private void ownerSLAHttp(String jsonBean) {
        HttpPresenter hp = new HttpPresenter(InitiatedWorkActivity.this);
        hp.setUrl(Api.OWNER_MINUTE_URL);
        hp.setshowDialogcontent("正在加载");
        hp.setRequsetId(1);
        hp.putParam("jsonBean", jsonBean);
        hp.setCallBack(this);
        hp.post();

    }

    //服务商工单 SLA协议
    private void serviceSLAHttp(String jsonBean) {
        HttpPresenter hp = new HttpPresenter(InitiatedWorkActivity.this);
        hp.setUrl(Api.SERVICE_MINUTE_URL);
        hp.setshowDialogcontent("正在加载");
        hp.setRequsetId(2);
        hp.putParam("jsonBean", jsonBean);
        hp.setCallBack(this);
        hp.post();

    }

    //业主 发起工单
    private void ownerHttp(String jsonBean) {
        HttpPresenter hp = new HttpPresenter(InitiatedWorkActivity.this);
        hp.setUrl(Api.OWNER_SAVE_URL);
        hp.setshowDialogcontent("正在提交");
        hp.setRequsetId(3);
        hp.putParam("jsonBean", jsonBean);
        hp.setCallBack(this);
        hp.post();
    }

    //服务商 发起工单
    private void serviceHttp(String jsonBean) {
        HttpPresenter hp = new HttpPresenter(InitiatedWorkActivity.this);
        hp.setUrl(Api.SERVICE_SAVE_URL);
        hp.setshowDialogcontent("正在提交");
        hp.setRequsetId(4);
        hp.putParam("jsonBean", jsonBean);
        hp.setCallBack(this);
        hp.post();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            Bundle bundle = data.getExtras();

            if (requestCode == 111) {
                tvCompanyName.setText(bundle.getString("companyName"));
                tvCompanyName.setTextColor(Color.parseColor("#333333"));
                bindFlagStr = bundle.getString("bindFlag");
                companyCodeStr = bundle.getString("companyCode");

                tvSponsor.setText("请选择");//发起人
                tvSponsor.setTextColor(Color.parseColor("#999999"));

                tvDealPeople.setText("请选择");
                tvDealPeople.setTextColor(Color.parseColor("#999999"));

                tvProject.setText("请选择");//所属项目
                tvProject.setTextColor(Color.parseColor("#999999"));

                tvServiceNew.setText("请选择");
                tvServiceNew.setTextColor(Color.parseColor("#999999"));

                tvService.setText("请选择");
                tvService.setTextColor(Color.parseColor("#999999"));

                tvType.setText("");

            } else if (requestCode == 222) {

                if (!TextUtils.isEmpty(bundle.getString("name"))) {
                    tvSponsor.setText(bundle.getString("name"));
                    tvSponsor.setTextColor(Color.parseColor("#333333"));
                }

            } else if (requestCode == 333) {
                if (!TextUtils.isEmpty(bundle.getString("companyName"))) {
                    tvDealCompanyName.setText(bundle.getString("companyName"));
                    tvDealCompanyName.setTextColor(Color.parseColor("#333333"));

                    tvDealPeople.setText("请选择");
                    tvDealPeople.setTextColor(Color.parseColor("#999999"));
                }
            } else if (requestCode == 444) {
                if (!TextUtils.isEmpty(bundle.getString("name"))) {
                    tvDealPeople.setText(bundle.getString("name"));
                    tvDealPeople.setTextColor(Color.parseColor("#333333"));
                }
            } else if (requestCode == 555) {
                if (!TextUtils.isEmpty(bundle.getString("contractName"))) {
                    tvProject.setText(bundle.getString("contractName"));
                    tvProject.setTextColor(Color.parseColor("#333333"));

                    tvServiceNew.setText("请选择");
                    tvServiceNew.setTextColor(Color.parseColor("#999999"));

                    tvService.setText("请选择");
                    tvService.setTextColor(Color.parseColor("#999999"));
                }

            } else if (requestCode == 666) {
                if (!TextUtils.isEmpty(bundle.getString("name"))) {
                    tvServiceNew.setText(bundle.getString("name"));
                    tvServiceNew.setTextColor(Color.parseColor("#333333"));
                }

            } else if (requestCode == 777) {

                if (!TextUtils.isEmpty(bundle.getString("directoryName"))) {
                    tvService.setText(bundle.getString("directoryName"));
                    tvService.setTextColor(Color.parseColor("#333333"));
                }

                if (!TextUtils.isEmpty(bundle.getString("contractName"))) {
                    tvService.setText(bundle.getString("contractName"));
                    tvService.setTextColor(Color.parseColor("#333333"));
                }

                if (!TextUtils.isEmpty(bundle.getString("completeTimes")) && !TextUtils.isEmpty(bundle.getString("type"))) {
                    if ("1".equals(bundle.getString("type"))) {
                        tvType.setText(bundle.getString("completeTimes") + "次");

                    } else if ("2".equals(bundle.getString("type"))) {

                        tvType.setText(bundle.getString("completeTimes") + "天");
                    }
                }
            }

        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onSuccess(int requestId, String result) {
        switch (requestId) {
            case 1:
                Log.e("业主  低 返回数据", result);
                OwnerSLABean ownerSLABean = new Gson().fromJson(result, OwnerSLABean.class);
                if (ownerSLABean.getMessage().getCode() == 200) {
                    etDay.setText(ownerSLABean.getRows().getCompleteDay());
                    etHour.setText(ownerSLABean.getRows().getCompleteHour());
                }
                break;
            case 2:
                Log.e("服务商  低 返回数据", result);
                ServiceSLABean serviceSLABean = new Gson().fromJson(result, ServiceSLABean.class);
                if (serviceSLABean.getMessage().getCode() == 200) {
                    etDay.setText(serviceSLABean.getRows().getCompleteDay());
                    etHour.setText(serviceSLABean.getRows().getCompleteHour());
                }
                break;
            case 3:
                Log.e("业主提发起工单",result);
                WorkBean ownerWorkBean = new Gson().fromJson(result,WorkBean.class);
                if (ownerWorkBean.getMessage().getCode() == 200){
                    if ("1".equals(ownerWorkBean.getRows().getOverLimit())){
                        ToastUtil.showShort(InitiatedWorkActivity.this,"本月工单数量超限!");
                    }else {
                        //发起工单dialog
                        new SVProgressHUD(this).showSuccessWithStatus("发起成功");
                    }
                }
                break;
            case 4:
                Log.e("服务商提发起工单",result);
                WorkBean serviceWorkBean = new Gson().fromJson(result,WorkBean.class);
                if (serviceWorkBean.getMessage().getCode() == 200){
                    if ("1".equals(serviceWorkBean.getRows().getOverLimit())){
                        ToastUtil.showShort(InitiatedWorkActivity.this,"本月工单数量超限!");
                    }else {
                        //发起工单dialog
                        new SVProgressHUD(this).showSuccessWithStatus("发起成功");
                    }
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void onException(int requestId, String code) {

    }
}
